#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QList>
#include <QListView>
#include <QButtonGroup>
#include <QMessageBox>
#include <QVariant>
#include <QFile>
#include <QDir>
#include <QFileDialog>
#include <QDebug>
#include <QObjectUserData>
#include <QSortFilterProxyModel>

#include "itemdef.h"
#include "cJSON.h"
#include "itemdelegate.h"
#include "threadhandle.h"
#include "threaddetect.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

signals:
    void signal_messageBox(int type, QString message);

public slots:
    void slot_testProc(int devCnt);
    void slot_testCmdSync(QString strSync);
    void slot_adbDetect(int devCount, bool needRefresh);
    void slot_adbSyncEnd();

private slots:
    void on_Btn_Test_A_clicked();
    void slot_messageBox(int type, QString message);

    void on_Btn_Reset_A_clicked();

    void on_checkBox_A_stateChanged(int arg1);

    void on_checkBox_B_stateChanged(int arg1);

    void on_checkBox_C_stateChanged(int arg1);

    void on_Btn_Test_B_clicked();

    void on_Btn_Reset_B_clicked();

    void on_Btn_Test_C_clicked();

    void on_Btn_OK_C_clicked();

    void on_Btn_NG_C_clicked();

    void on_Btn_Reset_C_clicked();

    void on_listView_C_clicked(const QModelIndex &index);

    void on_listView_A_clicked(const QModelIndex &index);

    void on_listView_B_clicked(const QModelIndex &index);

    void on_pushBtn_Entry_clicked();

    void on_pushBtn_Exit_clicked();

    void on_pushBtn_Reload_clicked();

    void on_Btn_Single_A_clicked();

private:
    Ui::MainWindow *ui;
    QButtonGroup *groupCheckbox;

    void textEditLoadText(QString fileName);
    ItemParseVal getTestItemsFromFile(QString fileName);
    void initListView(int totalItems, QList<ItemDesc> *list,
                      QStandardItemModel *model,
                      QSortFilterProxyModel *proxyModel,
                      ItemDelegate *delegate,
                      QListView *listView);
    void setItemStatus(QModelIndex &index, TestStatus testStat,
                       QStandardItemModel *model,
                       QSortFilterProxyModel *proxyModel);
    void resetAllItems(QListView *listView,
                      QStandardItemModel *model,
                      QSortFilterProxyModel *proxyModel);
    void switchTestingType(TestingType type);
    TestingType getTestingType();
    void enableBttons_C(bool enable);

    ItemDelegate *m_delegateA;
    QSortFilterProxyModel* m_proxyModelA;
    QStandardItemModel *m_modelA;

    ItemDelegate *m_delegateB;
    QSortFilterProxyModel* m_proxyModelB;
    QStandardItemModel *m_modelB;

    ItemDelegate *m_delegateC;
    QSortFilterProxyModel* m_proxyModelC;
    QStandardItemModel *m_modelC;

    QList<ItemDesc> *m_listA;
    QList<ItemDesc> *m_listB;
    QList<ItemDesc> *m_listC;
    int m_totalItemsA = 0;
    int m_totalItemsB = 0;
    int m_totalItemsC = 0;

    QString m_cmdEnterTestMode;
    QString m_cmdExitTestMode;
    QString m_strTestHault;
    QString m_strTesting;
    QString m_strTestOK;
    QString m_strTestNG;
    QString m_strWaitJudge;

    QString m_strADBConnected = "ADB设备已连接";
    QString m_strADBDisconnect = "无ADB设备";
    QString m_strADBMultiple = "多个ADB设备";
    QString m_strStartFailed = "测试项启动失败";
    QString m_strErrorMode = "未进入测试模式";

    QString m_fileWelcome = "./configs/welcome.txt";
    QString m_fileDefaultConf = "./configs/testItems.json";
    QString m_dirDefaultConf = "./configs";

    QString m_fileHistory;
    QString m_dirStandard; // C:\Users\<xxxx>\Documents\PCBATestTool

    bool m_bTestSingleA = false;
    ThreadHandle *m_thread;
    ThreadHandle *m_thread2;
    TestingType m_testingType = TT_NONE;

    ThreadDetect *m_trdDetect;
};
#endif // MAINWINDOW_H
